home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / orca / httpserver.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  4.9 KB  |  123 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Provides an HTTP server for Orca.  This currently serves mainly as
  5. something that self-voicing applications can use as their speech
  6. service.'''
  7. __id__ = '$Id: httpserver.py 3882 2008-05-07 18:22:10Z richb $'
  8. __version__ = '$Revision: 3882 $'
  9. __date__ = '$Date: 2008-05-07 14:22:10 -0400 (Wed, 07 May 2008) $'
  10. __copyright__ = 'Copyright (c) 2006-2008 Sun Microsystems Inc.'
  11. __license__ = 'LGPL'
  12. import threading
  13. import BaseHTTPServer
  14. import debug
  15. import platform
  16. import settings
  17. import speech
  18. _httpRequestThread = None
  19. loggingFileHandlers = { }
  20. loggingStreamHandlers = { }
  21.  
  22. class _HTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
  23.     """Provides support for communicating with Orca via HTTP.  This is
  24.     mainly to support self-voicing applications that want to use Orca
  25.     as a speech service.
  26.  
  27.     The protocol is simple: POST content is 'stop', 'speak:<text>',
  28.     or 'isSpeaking'.
  29.  
  30.     To test this, run:
  31.  
  32.       wget --post-data='speak:hello world' localhost:20433
  33.  
  34.     """
  35.     
  36.     def log_request(self, code = None, size = None):
  37.         '''Override to avoid getting a log message on stdout for
  38.         each GET, POST, etc. request'''
  39.         pass
  40.  
  41.     
  42.     def do_GET(self):
  43.         self.send_response(200)
  44.         self.send_header('Content-type', 'text/html')
  45.         self.end_headers()
  46.         self.wfile.write('<html><body><p>Orca %s</p></body></html>' % platform.version)
  47.  
  48.     
  49.     def do_POST(self):
  50.         contentLength = self.headers.getheader('content-length')
  51.         if contentLength:
  52.             contentLength = int(contentLength)
  53.             inputBody = self.rfile.read(contentLength)
  54.             debug.println(debug.LEVEL_FINEST, 'httpserver._HTTPRequestHandler received %s' % inputBody)
  55.             if inputBody.startswith('speak:'):
  56.                 speech.speak(inputBody[6:])
  57.                 self.send_response(200, 'OK')
  58.             elif inputBody == 'stop':
  59.                 speech.stop()
  60.                 self.send_response(200, 'OK')
  61.             elif inputBody == 'isSpeaking':
  62.                 self.send_response(200, 'OK')
  63.                 self.send_header('Content-type', 'text/html')
  64.                 self.end_headers()
  65.                 self.wfile.write('%s' % speech.isSpeaking())
  66.             
  67.         else:
  68.             debug.println(debug.LEVEL_FINEST, 'httpserver._HTTPRequestHandler received no data')
  69.  
  70.  
  71.  
  72. class _HTTPRequestThread(threading.Thread):
  73.     '''Runs a _HTTPRequestHandler in a separate thread.'''
  74.     
  75.     def run(self):
  76.         '''Try to start an HTTP server on settings.httpServerPort.
  77.         If this fails, retry settings.maxHttpServerRetries times,
  78.         each time incrementing the server port number by 1. If we
  79.         are still unable to start a server, just fail gracefully.
  80.         '''
  81.         portNo = settings.httpServerPort
  82.         connected = False
  83.         while not connected and portNo < settings.httpServerPort + settings.maxHttpServerRetries:
  84.             
  85.             try:
  86.                 httpd = BaseHTTPServer.HTTPServer(('', portNo), _HTTPRequestHandler)
  87.                 connected = True
  88.             continue
  89.             if portNo == settings.httpServerPort:
  90.                 debug.printException(debug.LEVEL_WARNING)
  91.             
  92.  
  93.             debug.println(debug.LEVEL_WARNING, 'httpserver._HTTPRequestThread unable to start server on port %d' % portNo)
  94.             portNo += 1
  95.             continue
  96.         if not connected:
  97.             debug.println(debug.LEVEL_WARNING, 'httpserver._HTTPRequestThread server startup failed.')
  98.         else:
  99.             httpd.serve_forever()
  100.  
  101.  
  102.  
  103. def init():
  104.     '''Creates an HTTP server that listens for speak commands from a
  105.     separate port defined by settings.httpServerPort.  We run this
  106.     as a daemon so it will die automatically when orca dies.'''
  107.     global _httpRequestThread
  108.     if settings.httpServerPort and not _httpRequestThread:
  109.         
  110.         try:
  111.             _httpRequestThread = _HTTPRequestThread()
  112.             _httpRequestThread.setDaemon(True)
  113.             _httpRequestThread.start()
  114.         debug.printException(debug.LEVEL_WARNING)
  115.  
  116.     
  117.  
  118.  
  119. def shutdown():
  120.     '''Stops the HTTP server.  [[[WDW - not implemented yet.]]]'''
  121.     pass
  122.  
  123.